//import angular core
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';

//import third package
import 'rxjs/add/operator/switchMap';

//import service
import { PhotoService } from '../../service/photo.service';

//import class
import { Photo } from '../../domain/photo';


@Component({
  selector: 'photo-detail',
  templateUrl: './photo-detail.component.html',
  styleUrls: ['./photo-detail.component.css'],
  providers: [PhotoService]
})
export class PhotoDetailComponent implements OnInit {

  photo: Photo;

  constructor(
    private route: ActivatedRoute,
    private location: Location,
    private photoService: PhotoService
  ) { }

  ngOnInit() {
    this.route.params
      .switchMap((params: Params) => this.photoService.getPhotoById(+params['id']))
      .subscribe(photo => this.photo = photo);
  }

  save(): void {
    this.photoService.updatePhoto(this.photo)
      .then(() => this.goBack());
  }

  goBack(): void {
    this.location.back();
  }

}
